package com.gitee.peigenlpy.actable.manager.handler;

import com.gitee.peigenlpy.actable.constants.Constants;
import com.gitee.peigenlpy.actable.manager.system.SysMysqlCreateTableManager;
import com.gitee.peigenlpy.actable.manager.system.SysMysqlCreateTableManagerImpl;
import com.gitee.peigenlpy.actable.manager.util.ConfigurationUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.bean.LifecycleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 启动时进行处理的实现类
 *
 * @author chenbin.sun
 */
@SuppressWarnings("restriction")
@Component("startUpHandler")
public class StartUpHandlerImpl implements StartUpHandler, LifecycleBean {

    private static final Logger log = LoggerFactory.getLogger(StartUpHandlerImpl.class);

    private ConfigurationUtil configurationUtil;

    private SysMysqlCreateTableManager sysMysqlCreateTableManager;

    /** 数据库类型：mysql */
    public static String MYSQL = "mysql";

    /** 数据库类型：oracle */
    public static String ORACLE = "oracle";

    /** 数据库类型：sqlserver */
    public static String SQLSERVER = "sqlserver";

    /** 数据库类型：postgresql */
    public static String POSTGRESQL = "postgresql";

    /** 数据库类型 */
    private static String databaseType = null;


    public StartUpHandlerImpl() {
        sysMysqlCreateTableManager = new SysMysqlCreateTableManagerImpl();
        configurationUtil = new ConfigurationUtil();
    }

    public void startHandler() {
        // 获取配置信息
        databaseType = configurationUtil.getConfig(Constants.ACTABLE_DATABASE_TYPE_KEY) == null ? MYSQL : configurationUtil.getConfig(Constants.ACTABLE_DATABASE_TYPE_KEY);

        // 执行mysql的处理方法
        if (MYSQL.equals(databaseType)) {
            log.info("databaseType=mysql，开始执行mysql的处理方法");
            sysMysqlCreateTableManager.createMysqlTable();
        } else if (ORACLE.equals(databaseType)) {
            log.info("databaseType=oracle，开始执行oracle的处理方法");
        } else if (SQLSERVER.equals(databaseType)) {
            log.info("databaseType=sqlserver，开始执行sqlserver的处理方法");
        } else if (POSTGRESQL.equals(databaseType)) {
            log.info("databaseType=postgresql，开始执行postgresql的处理方法");
        } else {
            log.info("没有找到符合条件的处理方法！");
        }
    }


    @Override
    public void start() throws Throwable {
        startHandler();
    }
}
